package edu.ynu.se.xiecheng.achitectureclass.service;

import edu.ynu.se.xiecheng.achitectureclass.dao.DeviceDao;
import edu.ynu.se.xiecheng.achitectureclass.entity.Device;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DeviceServiceImpl implements IDeviceService {

    @Autowired
    private DeviceDao deviceDao;

    @Override
    public Device save(Device device) {
        return deviceDao.save(device);
    }

    @Override
    public Device findById(Long id) {
        return deviceDao.findById(id).orElse(null);
    }

    @Override
    public List<Device> findAll() {
        return deviceDao.findAll();
    }

    @Override
    public void delete(Long id) {
        Device device = findById(id);
        if (device != null) {
            device.setIsDeleted(1);
            deviceDao.save(device);
        }
    }

    @Override
    public List<Device> findByGreenhouseId(Long greenhouseId) {
        return deviceDao.findByGreenhouseId(greenhouseId);
    }

    @Override
    public Device findBySerialNumber(String serialNumber) {
        return deviceDao.findBySerialNumber(serialNumber);
    }

    @Override
    public Device findByDeviceId(String deviceId) {
        return deviceDao.findByDeviceId(deviceId);
    }

    @Override
    public List<Device> findByGreenhouseIdAndCategory(Long greenhouseId, Integer category) {
        return deviceDao.findByGreenhouseIdAndCategory(greenhouseId, category);
    }
} 